package com.ptszyxx.popose.module.base.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class UserBirthdayVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> birthday = new ObservableField<>();

    public UserBirthdayVM(@NonNull Application application, CommonRepository repository) {
        super(application, repository);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onBirthDayEvent = new SingleLiveEvent<>();
    }


    public BindingCommand onSubmitCommand = new BindingCommand(() -> requestUserUpdate());
    public BindingCommand onBirthDayCommand = new BindingCommand(() -> uc.onBirthDayEvent.call());
    public BindingCommand onBackCommand = new BindingCommand(() -> YActivityUtil.getInstance().jumpUserNick(this));


    private void requestUserUpdate() {
        if (!check())return;
        Map<String, String> map = new HashMap<>();
        map.put("birthday", birthday.get());
        HttpUtils.getInstance()
                .data(model.userEditBirthday(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YActivityUtil.getInstance().jumpUserSex(UserBirthdayVM.this, birthday.get());
                                YActivityUtil.getInstance().close(UserBirthdayVM.this);
                            }
                        });
    }


    private boolean check(){
        if (YStringUtil.isEmpty(birthday.get())){
            YToastUtil.showShort(R.string.et_birthday_hint);
            return false;
        }
        return true;
    }
}
